// pages/votedetail/votedetail.js
Page({
  data: {},
  onLoad(options) {
    this.setData({
      id: options.id
    });
    this.getNet();
  },
  getNet() {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    wx.request({
      url: 'https://api.dianshiyexiao.com/weapp/company/query',
      method: 'POST',
      data: {
        id: that.data.id
      },
      success(res) {
        wx.hideLoading()
        console.log(res.data)
        if (res.data.data) {
          //渲染数据
          that.setData({
            name: res.data.data.name,
            legal: res.data.data.legal,
            region: res.data.data.region,
            // contact_name: res.data.data.contact_name,
            // contact_mobile: res.data.data.contact_mobile,
            product_name: res.data.data.product_name,
            product_cover: that.stringToImgList(res.data.data.product_cover),
            product_description: that.stringToImgList(res.data.data.product_description),
            logo: that.stringToImgList(res.data.data.logo),
            company_introduction: that.stringToImgList(res.data.data.company_introduction),
          })
          wx.setNavigationBarTitle({
            title: that.data.product_name,
          })
        } else {

        }
      },
      fail(error) {
        wx.hideLoading()
        console.log(error)
      },
    })
  },
  stringToImgList(str) {
    var list = [];
    var array = str.split(",")
    array.forEach(item => {
      list.push({
        "url": item
      })
    });
    return list;
  },
  goHome() {
    wx.reLaunch({
      url: '/pages/index2/index2',
    })
  },
  toupiaoNet() {
    wx.showLoading({
      title: '投票中',
    })
    var that = this;
    wx.request({
      url: 'https://api.dianshiyexiao.com/weapp/company/vote',
      method: 'POST',
      header: {
        'Authorization': wx.getStorageSync('token') // 默认值
      },
      data: {
        id: that.data.id
      },
      success(res) {
        wx.hideLoading();
        console.log(res.data)
        if (res.data.status == 200) {
          wx.showToast({
            title: "投票成功",
            icon: "success"
          })
        } else if (res.data.status == 410000 || res.data.message == "请登录") {
          wx.showToast({
            title: res.data.message,
            icon: 'error'
          })
          wx.navigateTo({
            url: '/pages/login/login',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'error'
          })
        }
      },
      fail(error) {
        wx.hideLoading();
        console.log(error)
      },
    })
  },
  onShareAppMessage() {
    var that = this;
    var cover=that.data.product_cover[0].url
    return {
      title: "快来为我投票吧!",
      path: '/pages/votedetail/votedetail?id='+that.data.id,
      imageUrl:cover
    }
  },
  onShareTimeline() {
    var that = this;
    return {
      title: "快来为我投票吧!",
      path: '/pages/votedetail/votedetail?id='+that.data.id,
    }
  },
})